package org.lee.pdf.impl;

import com.itextpdf.text.pdf.FdfReader;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.lee.pdf.IPdfExporter;
import org.lee.pdf.model.ApiResult;
import org.lee.pdf.model.ResultCode;
import org.lee.pdf.utils.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * @author jackielee
 * @version 1.0
 * @description PdfExporter
 * @date 2021/5/7
 */
public class PdfExporter implements IPdfExporter {

  @Override
  public ApiResult export(String fileName, String outputPath) {
    File file = new File(fileName);
    if (!file.exists() || !file.isFile() || !FileUtils.checkPdf(file)) {
      return ApiResult.failure(ResultCode.INVALID, "the pdf file was not exists");
    }

    try (XWPFDocument doc = new XWPFDocument()) {
      PdfReader reader = new FdfReader(fileName);
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);
      for (int i = 1; i <= reader.getNumberOfPages(); ++i) {
        TextExtractionStrategy strategy =
            parser.processContent(i, new SimpleTextExtractionStrategy());
        String text = strategy.getResultantText();
        XWPFParagraph p = doc.createParagraph();
        XWPFRun run = p.createRun();
        run.setText(text);
        run.addBreak(BreakType.PAGE);
      }

      return ApiResult.success();
    } catch (IOException e) {
      e.printStackTrace();
      return ApiResult.failure(ResultCode.ERROR, e.getMessage());
    }
  }

  public ApiResult export2(String fileName, String outputPath) {
    File file = new File(fileName);
    if (!file.exists() || !file.isFile() || !FileUtils.checkPdf(file)) {
      return ApiResult.failure(ResultCode.INVALID, "the pdf file was not exists");
    }

    try (PDDocument doc = Loader.loadPDF(file)) {
      int pageCount = doc.getNumberOfPages();
      File docFile = new File(outputPath);
      if (docFile.exists()) {
        docFile.delete();
      }
      try (FileOutputStream fos = new FileOutputStream(docFile)) {
        try (OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8")) {
          PDFTextStripper stripper = new PDFTextStripper();
          stripper.setSortByPosition(true); // sort
          stripper.setStartPage(1); // from
          stripper.setEndPage(pageCount); // to
          stripper.writeText(doc, writer);
          return ApiResult.success();
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      return ApiResult.failure(ResultCode.ERROR, "covert to doc failure:" + e.getMessage());
    }
  }
}
